//
//  CYHHeaderView.m
//  Cat
//
//  Created by dllo on 15/11/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CYHHeaderView.h"

@interface CYHHeaderView ()

@property (nonatomic, strong) NSMutableArray *headArr;

@end

@implementation CYHHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(30, 30);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.minimumLineSpacing = 5;
        flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        
        self.commentsCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
        self.commentsCollection.delegate = self;
        self.commentsCollection.dataSource = self;
        self.commentsCollection.backgroundColor = [UIColor clearColor];
        [self addSubview:self.commentsCollection];
        self.commentsCollection.scrollEnabled = NO;
        
        [self.commentsCollection registerClass:[CYHCommentCell class] forCellWithReuseIdentifier:@"comments"];
        
    }
    
    return self;
}



#pragma mark - Data
- (void)setHeaderModel:(HotModel *)HeaderModel {
    self.headArr = [NSMutableArray array];
    if (HeaderModel.likeUids != nil) {
        
        for (NSString *str in HeaderModel.likeUids) {
            // 个人信息
            [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://user.huapipi.com/userinfo/basic.do?uid=%@", str] block:^(id result) {
                [self.headArr addObject:result[@"portrait"]];
                
                if (self.headArr.count == 8) {
                    
                    [self.commentsCollection reloadData];
                    
                }
            }];
        }
    }
}



#pragma mark - collection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.headArr.count > 9) {
        return 8;
    } else {
        
        return self.headArr.count;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CYHCommentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"comments" forIndexPath:indexPath];
    
    [cell.headerImageView sd_setImageWithURL:[NSURL URLWithString:self.headArr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];
    return cell;
}

@end
